﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using System.Data;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS_Global;

public partial class IFRS_WIN29_2E : PageBase
{
    #region "---------- Property -----------"

    private readonly SPR_DOUBTFUL dal_doubtful = new SPR_DOUBTFUL();
    private Doubtful doubtful = new Doubtful();

    #endregion

    #region "---------- Events -----------"

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            SetErrorMessage();
            BindData();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }

    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN29_1S.aspx");
    }

    #endregion

    #region "----- Methods -------"

    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsResource.Edit + Resources.ifrsMenu.WIN29_2E;
    }

    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        reqTransactionDate.SetRequiredField(lblTransactionDate, txtTransactionDate, btnSAVE);
        reqPass.SetRequiredField(lblPass, txtPass, btnSAVE);
        reqSpecialMention.SetRequiredField(lblSpecialMention, txtSpecialMention, btnSAVE);
        reqSubstandard.SetRequiredField(lblSubstandard, txtSubstandard, btnSAVE);
        reqDoubtfulValue.SetRequiredField(lblDoubtfulValue, txtDoubtfulValue, btnSAVE);
        reqDoubtfulLoss.SetRequiredField(lblDoubtfulLoss, txtDoubtfulLoss, btnSAVE);
    }

    private void BindData()
    {
        int ID = Convert.ToInt32(Request.QueryString["id"].ToString());
        DataTable dtDOUBTFUL = dal_doubtful.SEARCH_BYID(ID);
        List<Doubtful> DOUBTFULList = new List<Doubtful>();
        ConvertHelper.ConvertDataTableToObject(dtDOUBTFUL, DOUBTFULList);
        if (dtDOUBTFUL.Rows.Count > 0)
        {
            hidDoubtfulID.Value = DOUBTFULList[0].DoubtfulID.ToString();
            txtTransactionDate.Text = DOUBTFULList[0].TransactionDate.DateToString();
            txtPass.Text = DOUBTFULList[0].Pass.ToString();
            txtSpecialMention.Text = DOUBTFULList[0].SpecialMention.ToString();
            txtSubstandard.Text = DOUBTFULList[0].Substandard.ToString();
            txtDoubtfulValue.Text = DOUBTFULList[0].DoubtfulValue.ToString();
            txtDoubtfulLoss.Text = DOUBTFULList[0].DoubtfulLoss.ToString();
        }
    }

    private void SaveData()
    {
        try
        {
            doubtful.DoubtfulID = Int32.Parse(hidDoubtfulID.Value);
            doubtful.TransactionDate = txtTransactionDate.Text.StringToDate();
            doubtful.Pass = txtPass.Text.AsDecimal();
            doubtful.SpecialMention = txtSpecialMention.Text.AsDecimal();
            doubtful.Substandard = txtSubstandard.Text.AsDecimal();
            doubtful.DoubtfulValue = txtDoubtfulValue.Text.AsDecimal();
            doubtful.DoubtfulLoss = txtDoubtfulLoss.Text.AsDecimal();
            doubtful.SetStandardField();

            int iID = dal_doubtful.UPDATE(doubtful);
            if (iID > 0)
            {
                this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN29_2E.aspx?&id={0}", hidDoubtfulID.Value)));
            }
            else
            {
                Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    #endregion

}